//
//  UILabel_Extension.swift
//  swiftProductTemplate
//
//  Created by 刘圣洁 on 2025/6/19.
//

import UIKit

extension UILabel {
    // MARK: - 快捷创建方法
    static func make(
        frame: CGRect? = nil,
        text: String? = nil,
        font: UIFont = AppTheme.Fonts.body,
        textColor: UIColor = AppTheme.Colors.textPrimary,
        alignment: NSTextAlignment = .left,
        numberOfLines: Int = 1,
        toView: UIView
    ) -> UILabel {
        let label = UILabel()
        if let frame { label.frame = frame }
        label.text = text
        label.font = font
        label.textColor = textColor
        label.textAlignment = alignment
        label.numberOfLines = numberOfLines
        toView.addSubview(label)
        return label
    }
    
    // MARK: - 链式配置方法
    @discardableResult
    func withFrame(_ frame: CGRect) -> Self {
        self.frame = frame
        return self
    }
    
    @discardableResult
    func withText(_ text: String?) -> Self {
        self.text = text
        return self
    }
    
    @discardableResult
    func withFont(_ font: UIFont) -> Self {
        self.font = font
        return self
    }
    
    @discardableResult
    func withTextColor(_ color: UIColor) -> Self {
        self.textColor = color
        return self
    }
    
    @discardableResult
    func withAlignment(_ alignment: NSTextAlignment) -> Self {
        self.textAlignment = alignment
        return self
    }
    
    @discardableResult
    func withNumberOfLines(_ numberOfLines: Int) -> Self {
        self.numberOfLines = numberOfLines
        return self
    }
    
    @discardableResult
    func withLineBreakMode(_ mode: NSLineBreakMode) -> Self {
        self.lineBreakMode = mode
        return self
    }
    
    @discardableResult
    func withAdjustsFontSize(toFit: Bool) -> Self {
        self.adjustsFontSizeToFitWidth = toFit
        return self
    }
    
    @discardableResult
    func withMinimumScaleFactor(_ factor: CGFloat) -> Self {
        self.minimumScaleFactor = factor
        return self
    }
    
    // MARK: - 动态类型支持
    @discardableResult
    func enableDynamicType() -> Self {
        self.adjustsFontForContentSizeCategory = true
        return self
    }
    
    // MARK: - 快速设置预设样式
    @discardableResult
    func asTitle() -> Self {
        self.font = AppTheme.Fonts.title
        self.textColor = AppTheme.Colors.textPrimary
        self.numberOfLines = 0
        self.adjustsFontForContentSizeCategory = true
        return self
    }
    
    @discardableResult
    func asSubtitle() -> Self {
        self.font = AppTheme.Fonts.subheadline
        self.textColor = AppTheme.Colors.textSecondary
        self.numberOfLines = 0
        self.adjustsFontForContentSizeCategory = true
        return self
    }
    
    @discardableResult
    func asBody() -> Self {
        self.font = AppTheme.Fonts.body
        self.textColor = AppTheme.Colors.textPrimary
        self.numberOfLines = 0
        self.adjustsFontForContentSizeCategory = true
        return self
    }
    
    @discardableResult
    func asCaption() -> Self {
        self.font = AppTheme.Fonts.caption1
        self.textColor = AppTheme.Colors.textSecondary
        self.numberOfLines = 0
        self.adjustsFontForContentSizeCategory = true
        return self
    }
}
